package com.lucun.es_transaction.config;


import com.lucun.es_transaction.util.JsonResult;
import com.lucun.es_transaction.util.LuCommonExceptionHandler;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class ConfExceptionHandlerBase {

    /**
     * 404处理
     *
     * @param ex      异常
     * @param request 请求
     * @return 处理异常结果
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Object notFountHandler(NoHandlerFoundException ex, HttpServletRequest request) {
        return LuCommonExceptionHandler.handException(ex, request);
    }

    /**
     * 异常处理
     *
     * @param ex      异常
     * @param request 请求
     * @return 处理异常结果
     */
    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public JsonResult<String> handException(Exception ex, HttpServletRequest request) {
        return LuCommonExceptionHandler.handException(ex, request);
    }

}
